{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset = pd.read_csv(\"data/table_4_3_watermelon_3_0_num.csv\")\n",
    "test_data = dataset.ix[0,1:-1]\n",
    "features = np.array(dataset.columns[1:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.52631579  0.47368421]\n"
     ]
    }
   ],
   "source": [
    "labels = np.unique(dataset.ix[:,-1])\n",
    "num_labels = len(labels)\n",
    "p_c = np.zeros(num_labels)\n",
    "for i in range(num_labels):\n",
    "    p_c[i] = (len(dataset[dataset['label']==labels[i]])+1)/(len(dataset)+num_labels)\n",
    "print(p_c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# the ith feature\n",
    "px_c = np.ones(num_labels)\n",
    "for i in range(len(test_data)):\n",
    "    column_name = dataset.columns[i+1]\n",
    "    for j in range(num_labels):\n",
    "        if dataset[column_name].dtype == 'float':\n",
    "            dataset_ci = dataset[dataset['label']==labels[j]]\n",
    "            mu = np.mean(dataset_ci[column_name])\n",
    "            sigma = np.std(dataset_ci[column_name])\n",
    "            px_c[j] *= 1/(np.sqrt(2*np.pi)*sigma)*np.exp(-pow((test_data[i]-mu),2)/(2*sigma*sigma))\n",
    "        else:\n",
    "            d_c = len(dataset[dataset['label']==labels[j]])\n",
    "            d_cx = len(dataset[(dataset['label']==labels[j])&(dataset[column_name]==test_data[i])])\n",
    "            n_i = len(np.unique(dataset[column_name]))\n",
    "            px_c[j] *= (d_cx+1)/(d_c+n_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.00083569  0.37062119]\n"
     ]
    }
   ],
   "source": [
    "px = 1/len(dataset)\n",
    "pc_x = np.zeros(num_labels)\n",
    "for i in range(num_labels):\n",
    "    pc_x[i] = p_c[i]*px_c[i]/px\n",
    "print(pc_x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
